package lox

class LoxInstance(private val klass: LoxClass, private val fields: MutableMap<String, Any?> = mutableMapOf()) {
    override fun toString(): String = "${klass.name} instance"

    operator fun get(name: Token): Any? = when {
        fields.containsKey(name.lexeme) -> fields[name.lexeme]
        klass.findMethod(name.lexeme) != null -> klass.findMethod(name.lexeme)?.bind(this)
        else -> throw RuntimeError(
            name, "Undefined property '${name.lexeme}'."
        )
    }

    operator fun set(name: Token, value: Any?) = fields.put(name.lexeme, value)
}
